﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;

namespace SmartSpace.WebApp
{
    public class SsPersonalizationProvider : PersonalizationProvider
    {
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override PersonalizationStateInfoCollection FindState(PersonalizationScope scope, PersonalizationStateQuery query, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query)
        {
            throw new NotImplementedException();
        }

        protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)
        {
            string queryString = webPartManager.Page.ClientQueryString;
            NameValueCollection query = HttpUtility.ParseQueryString(queryString);
            string name = query["ss:page"];
            string id = query["ss:pageid"];

            if (!string.IsNullOrEmpty(name))
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                PageContent personalization = service.GetPagePersonalization(SsSite.Current.Id, name);

                if (personalization != null)
                {
                    userDataBlob = personalization.Data;
                    sharedDataBlob = personalization.Data;
                }
            }
            else if (!string.IsNullOrEmpty(id))
            {
                Guid pageId = new Guid(id);
                ICoreService service = ServiceLocator.Get<ICoreService>();
                PageContent personalization = service.GetPageContent(SsSite.Current.Id, pageId);

                if (personalization != null)
                {
                    userDataBlob = personalization.Data;
                    sharedDataBlob = personalization.Data;
                }
            }
        }

        protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName)
        {
            throw new NotImplementedException();
        }

        public override int ResetState(PersonalizationScope scope, string[] paths, string[] usernames)
        {
            throw new NotImplementedException();
        }

        public override int ResetUserState(string path, DateTime userInactiveSinceDate)
        {
            throw new NotImplementedException();
        }

        protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob)
        {
            //
            string queryString = webPartManager.Page.ClientQueryString;
            string strPageId = HttpUtility.ParseQueryString(queryString)["ss:pageid"];
            if (!string.IsNullOrEmpty(strPageId))
            {
                Guid pageId = new Guid(strPageId);
                ICoreService service = ServiceLocator.Get<ICoreService>();

                PageContent personalization = service.GetPageContent(SsSite.Current.Id, pageId);
                if (personalization == null)
                {
                    personalization = new PageContent();
                    personalization.Id = pageId;
                    personalization.SiteId = SsSite.Current.Id;
                    personalization.PageId = pageId;
                    personalization.Data = dataBlob;
                    service.SavePageContent(personalization);
                }
                else
                {
                    personalization.Data = dataBlob;
                    service.SavePageContent(personalization);
                }
            }
        }
    }
}